package com.lottery.game.controller.v2.user;

import com.lottery.game.annotation.Current;
import com.lottery.game.common.Response;
import com.lottery.game.constants.RespErrorCodes;
import com.lottery.game.controller.v2.user.dto.GameUserDetail;
import com.lottery.game.controller.v2.user.dto.UpdateUserPayPwdRequest;
import com.lottery.game.controller.v2.user.dto.UpdateUserPwdRequest;
import com.lottery.game.core.model.dto.UserToken;
import com.lottery.game.core.modules.entity.user.GameUser;
import com.lottery.game.core.modules.service.app.GameUserService;

import com.lottery.game.core.modules.service.game.SysDictService;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;


/**
 * @author : xxxx
 * @version : 1.0
 * @date : 2019-02-27 10:11
 * @Description :
 * @Copyright : Copyright (c) 2018
 * @Company : xxxx Technology(Beijing) Chengdu Co. Ltd.
 * @link : com.runshang.apps.cloudgame.controller.v2.user.UserDetailController
 */
@Slf4j
@RestController
@RequestMapping(value = "api/v2/user")
@ResponseBody
@Valid
@Api(tags="APP-用户详情-ok")
public class UserDetailController {

    private String imageServer;

    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private GameUserService gameUserService;
    private String imageServerAddress;

    @GetMapping("detail")
    @ApiOperation(notes = "用户详细信息", value = "用户详细信息-ok")
    @ApiImplicitParam(name = "ticket", value = "ticket",dataType = "String",paramType="header",required = true)
    public Response<GameUserDetail> detail(@ApiIgnore @Current UserToken ticket) {
        GameUser gameUser = gameUserService.get(ticket.getId());
        String imageServer = this.getImageServer();
        if(gameUser != null && !StringUtils.isEmpty(gameUser.getPhotourl())){
            gameUser.setPhotourl(imageServer+gameUser.getPhotourl());
        }
        return Response.build(gameUser.convert(GameUserDetail.class));
    }

    /**
     * 修改用户密码
     *
     * @param ticket
     * @param request
     * @return
     */
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "修改用户登陆密码-ok", notes = "更新用户个人密码，必须携带令牌(header)ticket, 密码长度为8到16位", httpMethod="POST")
    @ApiImplicitParam(name = "ticket", value = "ticket",dataType = "String",paramType="header",required = true)
    @PostMapping(value = "/password/update", produces= {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ResponseBody
    public Response<String> passwordUpdate(@ApiIgnore @Current UserToken ticket,
                                           @RequestBody @Valid  UpdateUserPwdRequest request) {
        try {
            if (request.getUserPwd().equals(request.getOldPwd())){
                return Response.buildError("新密码不能跟旧密码相同");
            }
            Integer i = gameUserService.updatePwd(ticket.getId(), ticket.getUsername(), request.getUserPwd(), request.getOldPwd());
            if (i == 0) {
                return Response.buildSuccess("修改密码成功");
            }else if (i == 1){
                return Response.buildError("修改密码失败!");
            }else {
                return Response.buildError("原密码错误!");
            }
        } catch (Exception e) {
            log.error(RespErrorCodes.UNKNOWN_ERROR.errorCode(), e);
            return Response.buildError("密码错误");
        }
    }


    /**
     * 修改支付密码
     *
     * @param request
     * @return
     */
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "修改用户支付密码-ok", notes = "修改用户支付密码，必须携带令牌(header)ticket, 密码长度为6位")
    @PostMapping(value = "/pay/password/update", produces= {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ApiImplicitParam(name = "ticket", value = "ticket",dataType = "String",paramType="header",required = true)
    @ResponseBody
    public Response<String> updateUserPayPwd(@ApiIgnore @Current UserToken ticket, @RequestBody @Validated  UpdateUserPayPwdRequest request) {
        try {
            if (request.getUserPayPwd().equals(request.getOldPayPwd())){
                return Response.buildError("新密码不能跟旧密码相同");
            }
            Integer i = gameUserService.updatePayPwd(ticket.getId(), ticket.getUsername(), request.getUserPayPwd(), request.getOldPayPwd());
            if ( i == 0) {
                return Response.buildSuccess("修改支付密码成功");
            } else if(i == 1){
                return Response.buildError("修改密码失败!");
            }else{
                return Response.buildError("原密码错误!");
            }
        } catch (Exception e) {
            log.error(RespErrorCodes.UNKNOWN_ERROR.errorCode(), e);
            return Response.buildError("密码错误");
        }
    }

    /**
     * 添加新的支付密码.目前禁用了
     * @param request
     * @return
     */
//    @SuppressWarnings("unchecked")
//    @ApiOperation(value = "新增支付密码", notes = "新增支付密码，需要ticket", httpMethod="POST")
//    @PostMapping(value = "/add/new/pay/password", produces= {MediaType.APPLICATION_JSON_UTF8_VALUE})
//    @ResponseBody
//    public Response<String> addNewPayPassword(@ApiIgnore @Current UserToken userToken, @RequestBody UpdatePayPwdRequest request) {
//        try {
//            if (gameUserService.updateNewPayPwd(userToken.getId(), userToken.getUsername(), request.getUserPayPwd())) {
//                return Response.buildSuccess("修改支付密码成功");
//            } else {
//                return Response.buildSuccess("修改支付密码失败");
//            }
//        } catch (Exception e) {
//            log.error(RespErrorCodes.UNKNOWN_ERROR.errorCode(), e);
//            return Response.buildSuccess("系统异常，请稍后再试");
//        }
//    }
    private String getImageServer() {
        if (imageServerAddress != null) {
            return imageServerAddress;
        }
        return imageServerAddress = sysDictService.findImageServerAddress();
    }

}
